import java.util.Arrays;

class MyArray{
    public int usedSize;
    public int[] elem;
    public static final int DEFAULT_SIZE = 2;
    public MyArray(){
        this.elem = new int[DEFAULT_SIZE];
    }
    public MyArray(int initCapacity){
        this.elem = new int[initCapacity];
    }

    public void display(){
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
    }
    public boolean isFull(){
        if(this.usedSize == this.elem.length){
            return true;
        }
        return false;
    }

    public void add(int data){
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[this.usedSize] = data;
        usedSize++;
    }

    public void add(int pos,int data){
        if(pos < 0 || pos > this.usedSize){
            throw new  PosOutOfBoundsException(pos + "位置不合法");
        }
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        for (int i = usedSize - 1; i >= pos ; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    public boolean contains(int data){
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == data){
                return true;
            }
        }
        return false;
    }

    public int indexOf(int data){
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == data){
                return i;
            }
        }
        return -1;
    }

    public int get(int pos){
        if(pos < 0 || pos >= this.usedSize){
            throw new PosOutOfBoundsException(pos + "位置不合法！");
        }
        return this.elem[pos];
    }

    public void set(int pos,int value) {
        if (pos < 0 || pos >= this.usedSize) {
            throw new PosOutOfBoundsException(pos + "位置不合法！");
        }
        this.elem[pos] = value;
    }

    public void remove(int data){
        int index = indexOf(data);
        if(index == -1){
            System.out.println("没有这个数据");
            return;
        }
        for (int i = index; i < this.usedSize; i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
    }

    public int size(){
        return usedSize;
    }

    public void clear(){
        this.usedSize = 0;
        //如果是引用类型
        /*for (int i = 0; i < this.usedSize; i++) {
            this.elem[i] = null;
        }
        this.elem[usedSize-1] = null;*/
    }
}
public class demo4 {
    public static void main(String[] args) {

    }
}
